package com.zmj.sy.mom.srv.aps.controller;

import cn.hutool.core.collection.CollUtil;
import com.zmj.sy.mom.srv.aps.bean.entity.base.BaseEntity;
import com.zmj.sy.mom.srv.aps.bean.entity.order.ApsOrder;
import com.zmj.sy.mom.srv.aps.bean.vo.apsbatch.ApsBatchListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.apsorder.ApsOrderScheduleReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseSelectResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.order.OrderScheduleReqVo;
import com.zmj.sy.mom.srv.aps.service.ApsBatchService;
import com.zmj.sy.mom.srv.aps.service.ApsOrderService;
import com.zmj.sy.mom.srv.aps.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@Api(tags = "排产批次")
@RestController
@RequestMapping("/apsBatch")
public class ApsBatchController {

    private final ApsBatchService apsBatchService;

    private final OrderService orderService;

    private final ApsOrderService apsOrderService;

    @ApiOperation("显示待完成的批次下拉框")
    @GetMapping("/select")
    public BaseSelectResVo select(){
        return apsBatchService.select();
    }


    @ApiOperation("列表")
    @GetMapping("/list")
    public BaseListResVo<ApsBatchListResVo> list(){
        apsBatchService.clearCache();
        return apsBatchService.list();
    }

    @ApiOperation("清空移单的缓存")
    @PostMapping("/clearCache")
    public BaseResVo clearCache(){
        apsBatchService.clearCache();
        return BaseResVo.ok();
    }
    @ApiOperation("移单的缓存")
    @PostMapping("/cache")
    public BaseResVo cache(@RequestBody List<ApsBatchListResVo> reqVo){
        apsBatchService.cache(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("移单的缓存列表")
    @GetMapping("/cacheList")
    public BaseListResVo<ApsBatchListResVo> cacheList(){
        return new BaseListResVo<>(apsBatchService.cacheList());
    }

    @ApiOperation("移单")
    @PostMapping("/move")
    public BaseResVo move(@RequestBody List<ApsBatchListResVo> reqVo){
        apsBatchService.clearCache();
        List<ApsBatchListResVo> move = apsBatchService.move(reqVo);

        // 拿出本条
        // 拿出所有(去除本条)

        // 全部重新排产
        for (int i = 0; i < move.size(); i++) {
            ApsBatchListResVo apsBatch = move.get(i);

            if(!CollUtil.isEmpty(apsBatch.getData())){
                continue;
            }

            OrderScheduleReqVo rrr = new OrderScheduleReqVo();
            rrr.setSchedulingRule(apsBatch.getSchedulingRule());
            List<ApsOrder> schedule = orderService.scheduleMove(rrr, apsBatch);


            ApsOrderScheduleReqVo reqVo1 = new ApsOrderScheduleReqVo();
            reqVo1.setIds(schedule.stream().map(BaseEntity::getId).collect(Collectors.toList()));
            reqVo1.setSchedulingRule(rrr.getSchedulingRule());
            reqVo1.setStartDateTime(LocalDateTime.of(LocalDate.now().plusDays(1), LocalTime.of(0, 0, 0)));
            try {

                // 锁定
                apsOrderService.lock(reqVo1);

                // 清理订单
                apsOrderService.clear(reqVo1.getIds());

                // 排产
                apsOrderService.scheduleMove(reqVo1, apsBatch);

                // 如果抛异常，则给未排产的解锁
                apsOrderService.unlock(reqVo1, 5);

            } catch (Exception e) {

                // 如果抛异常，则给未排产的解锁
                apsOrderService.unlock(reqVo1, 3);

                // 清理订单
                apsOrderService.clear(reqVo1.getIds());

                log.error(e.getMessage(), e);
                throw e;
            }
            orderService.publish();
        }



        return BaseResVo.ok();
    }
}
